#include <stdio.h>
#include <unistd.h>

#include "ohos_init.h"
#include "cmsis_os2.h"

#include "gd32h7xx.h"
#include "systick.h"
#include "gd32h759i_eval.h"

#include "key.h"

#define THREAD_DELAY_1S 1000000

void test(void)
{   

    gd_eval_led_init(LED1);
    gd_eval_led_init(LED2);
    key_init();                             /* 初始化按键 */
    
    uint8_t key;
    while(1)
    {        
        key = key_scan(0);                  /* 按键扫描得到键值:0:不支持连续按;1:支持连续按 */
      
        if (key)
        {
            switch (key)
            {
                case KEY0_PRES:               /* 控制LED0(RED)翻转 */
                    gd_eval_led_toggle(LED1); /* LED0状态取反 */ 
                    printf("LED1\n");
                    usleep(100*1000);
                    break;

                case WKUP_PRES:               /* 控制LED1(GREEN)翻转 */ 
                    gd_eval_led_toggle(LED2); /* LED1状态取反 */
                    printf("LED2\n");
                    usleep(100*1000);
                    break;
            } 
        }
        else
        {
            usleep(10*1000);
        }
    }

}

static void testTask(void)
{
    osThreadAttr_t attr;

    attr.name = "led_test";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 20*1024;
    attr.priority = osPriorityNormal;

    if (osThreadNew((osThreadFunc_t)test, NULL, &attr) == NULL) {
        printf("[testTask] Falied to create Task!\n");
    }
}
APP_FEATURE_INIT(testTask);